package cn.imvc.app.jobs;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;

import cn.imvc.app.entities.Commission;
import cn.imvc.app.entities.Dealer;
import cn.imvc.app.models.CommissionModel;
import cn.imvc.app.models.DealerLevelModel;
import cn.imvc.app.models.DealerModel;

@DisallowConcurrentExecution
public class TeamCommissionQuarz {
	@Autowired
	private DealerModel dealerModel;
	@Autowired
	private CommissionModel commissionModel;
	@Autowired
	private DealerLevelModel dealerLevelModel;

	public void teamCommission(JobExecutionContext context) {
		Calendar calendar = Calendar.getInstance();// 日历对象
		calendar.add(Calendar.MONTH, -1);// 月份减一
		String theDate = new SimpleDateFormat("yyyy-MM").format(calendar.getTime());// 输出上个月的日期

		/*
		 * Calendar calendar1 = Calendar.getInstance();
		 * calendar1.set(Calendar.DAY_OF_MONTH, 1);
		 * calendar1.set(Calendar.HOUR_OF_DAY,0);
		 * calendar1.set(Calendar.MINUTE,0); calendar1.set(Calendar.SECOND,0);
		 * calendar1.set(Calendar.MILLISECOND,0);
		 * 
		 * Calendar calendar2 = Calendar.getInstance();
		 * calendar2.add(Calendar.MONTH, +1);
		 * calendar2.set(Calendar.DAY_OF_MONTH, 1);
		 * calendar2.set(Calendar.HOUR_OF_DAY,0);
		 * calendar2.set(Calendar.MINUTE,0); calendar2.set(Calendar.SECOND,0);
		 * calendar2.set(Calendar.MILLISECOND,0);
		 * 
		 * List<Dealer> dealerList =
		 * dealerModel.findByDealerLevelNot(dealerLevelModel.findByLevel(1).get(
		 * 0)); for (Dealer dealer : dealerList) {
		 *//**
			 * 如果此月分佣过了，不在分佣
			 *//*
			 * List<Commission> commissions; commissions =
			 * commissionModel.findAllByMemberAndCreatedAtBetween(dealer.
			 * getMember(),calendar1.getTime(),calendar2.getTime());
			 * if(commissions.isEmpty()){
			 * dealerModel.updateTeamCommission(dealer,theDate); } }
			 * System.out.println("团队分佣完毕!!");
			 */

		List<Dealer> dealerList = dealerModel.findByDealerLevelOrDealerLevel(dealerLevelModel.findByLevel(3).get(0),dealerLevelModel.findByLevel(4).get(0));
		for (Dealer dealer : dealerList) {
			dealerModel.updateTeamCommission(dealer, theDate);
		}
		System.out.println("团队分佣完毕!!");
	}

}
